package com.qianya.oa.config.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;

import static org.fusesource.jansi.Ansi.ansi;

/**
 * @author LLL
 */
public class AnsiLevel extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent arg0) {
        Ansi.setEnabled(true);
        if (arg0.getLevel() == Level.INFO) {
            return ansi().bg(Color.GREEN).fgBright(Color.WHITE).a(arg0.getLevel().toString()).reset().toString();
        } else if (arg0.getLevel() == Level.DEBUG) {
            return ansi().bg(Color.MAGENTA).fgBright(Color.WHITE).a(arg0.getLevel().toString()).reset().toString();
        } else if (arg0.getLevel() == Level.WARN) {
            return ansi().bg(Color.YELLOW).fgBright(Color.WHITE).a(arg0.getLevel().toString()).reset().toString();
        } else if (arg0.getLevel() == Level.ERROR) {
            return ansi().bgBright(Color.RED).fgBright(Color.WHITE).a(arg0.getLevel().toString()).reset().toString();
        }
        return arg0.getLevel().toString();
    }

}
